"  CompileProject: - A rather specific .Net compiling automation script
"  -------------------------------------------------------------------
"  Version: 0.01a
"  Author: Albie Janse van Rensburg
"  Date Last Modified: 2009-06-23
"  Description: A plugin that executes the compilation script as specified
"  	and returns the compiler output in an output window.  Preconfigured to 
"  	compile the first "eikosonline.net.vbproj" file searching upwards in the 
"  	current file's directory tree.

" Load Once: {{{1
if exists('g:loaded_compile_project') || &compatible
	finish
endif
let g:loaded_compile_project = 1

" Variables: {{{1
let s:compilercommand = 'c:\Program\ Files\Microsoft\ Visual\ Studio\ 9.0\Common7\IDE\devenv.exe' " set the path to devenv
let s:projectfilename = 'eikosonline.net.sln' " the name of the project file to search for
let s:compargdebug = '/build debug'
let s:compargrelease = '/build release'
let s:outputwindowname = 'Build Output' " window title
let s:outputwindowsize = 15 " amount of lines to use for the initial output window

" Functions: {{{1
function! Compile_Project(...)
	" lets find that project file first
	let curwinnr = winnr()
	let projectfilefound = 0
	let pathtemp = &path
	exec("setlocal path=".escape(getcwd(), ' \').";")
	let projectfilename = findfile( s:projectfilename, ".;" )
	exec("setlocal path=".pathtemp)
	if projectfilename != ''
		let projectfilefound = 1
	else
		if filereadable( s:projectfilename ) == 1
			let projectfilename = getcwd() . '\' . s:projectfilename
			let projectfilefound = 1
		endif
	endif

	if projectfilefound == 1
		" execute the compiling mechanism!
		let fullcommand = s:compilercommand
		let fullcommand .= ' "' . projectfilename . '"'
		if (a:0 == 0) || (a:1 == 'debug')
			let fullcommand .= ' ' . s:compargdebug
		elseif (a:1 == 'release')
			let fullcommand .= ' ' . s:compargrelease
		endif
		
		let output = system(fullcommand)
		" echo output

		" put the results in an output window
		let buf_exists  = bufexists(bufnr(s:outputwindowname))
		let res_buf_nr = bufnr(s:outputwindowname)
		let buf_win_nr = bufwinnr(res_buf_nr)
		if buf_win_nr == -1
			" the output window is not visible, or has not been loaded yet
			silent exec 'bot ' . s:outputwindowsize . 'split '
			if res_buf_nr == -1
				" the output window has no buffer
				exec ":silent! e " . escape(s:outputwindowname, ' ')
			else
				" the output window has a buffer, but its not visible
				exec ":silent! buffer " . res_buf_nr
			endif
		else
			" the output window exists
	        exec buf_win_nr . "wincmd w"
		endif
		" delete all contents in the output window
		%d
		" print the output contents to the buffer
		silent! exec "put = l:output"
		setlocal nomodified
		setlocal nowrap
		setlocal noswapfile
		setlocal nonumber
		norm gg
		exec curwinnr . "wincmd w"
	else
		echo 'Could not find project file'
	endif
endfunction

" Shortcuts: {{{1
command! -nargs=? CompileProject call Compile_Project(<args>)
nnoremap <leader>cd :CompileProject "debug"<cr>
nnoremap <leader>cr :CompileProject "release"<cr>

" Modelines: {{{1
" vim: ts=4 nowrap fdm=marker
